#include <QCoreApplication>
#include <QFile>
#include <QDebug>
#include <QDataStream>

const QString fileName = "/home/luchu/stu.txt";

class Student{

public:
    int ID;
    QString Name;

    friend QDataStream& operator <<(QDataStream& out, const Student& stu)
    {
        out << stu.ID << stu.Name;
        return out;
    }

    friend QDataStream& operator >>(QDataStream& in, Student& stu)
    {
        stu = Student();
        in >> stu.ID >> stu.Name;
        return in;
    }
};

void Save()
{
   QFile file(fileName);
   if (!file.open(QIODevice::WriteOnly)){
       qDebug() << "Could not open file: " << fileName
                << ", " << file.errorString();
       return ;
   }
   QDataStream out(&file);
   Student s1, s2;
   s1.ID = 100;
   s1.Name ="Jack";
   s2.ID = 101;
   s2.Name = "Rose";
   out << s1 << s2;

   file.flush();
   file.close();
}

void Load()
{
    QFile file(fileName);
    if (!file.open(QIODevice::ReadOnly)){
        qDebug() << "Could not open file: " << fileName
                 << ", " << file.errorString();
        return ;
    }

    QDataStream in(&file);
    Student s1, s2;
    in >> s1 >> s2;


    qDebug() << "ID: " << s1.ID << ", " << s2.ID;
    qDebug() << "Name: " << s1.Name << ", " << s2.Name;

}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    Save();
    Load();

    return a.exec();
}
